package org.hawk.gwt.ppc.reflect.lang;

import junit.framework.TestCase;

import org.hawk.gwt.ppc.reflect.emul.java.lang.reflect.Constructor;
import org.hawk.gwt.ppc.reflect.lang.mock.Mock1;
import org.hawk.gwt.ppc.reflect.lang.mock.Mock2;
import org.hawk.gwt.ppc.reflect.lang.mock.Mock3;

public class ConstructorTestCase extends TestCase {
	protected void setUp() throws Exception {
		ReflectionTestHelper.initialize();
	}
	
	public void testGetConstructors(){
		assertEquals(1,ClassDelegate.forClass(Mock1.class).getConstructors().length);
		assertEquals(0,ClassDelegate.forClass(Mock2.class).getConstructors().length);
		assertEquals(1,ClassDelegate.forClass(Mock3.class).getConstructors().length);
	}
	
	public void testGetConstructor() throws SecurityException, NoSuchMethodException{
		assertNotNull(ClassDelegate.forClass(Mock1.class).getConstructor());
		assertNotNull(ClassDelegate.forClass(Mock3.class).getConstructor());
		try{
			assertNotNull(ClassDelegate.forClass(Mock2.class).getConstructor());
			fail();
		}
		catch(NoSuchMethodException ex){}
		try{
			assertNotNull(ClassDelegate.forClass(Mock3.class)
					.getConstructor(Mock3.class));
			fail();
		}
		catch(NoSuchMethodException ex){}
	}
	
	public void testGetDeclaredConstructors(){
		assertEquals(1,ClassDelegate.forClass(Mock1.class).getDeclaredConstructors().length);
		assertEquals(0,ClassDelegate.forClass(Mock2.class).getDeclaredConstructors().length);
		assertEquals(3,ClassDelegate.forClass(Mock3.class).getDeclaredConstructors().length);
	}
	
	public void testGetDeclaredConstructor() throws SecurityException, NoSuchMethodException{
		assertNotNull(ClassDelegate.forClass(Mock1.class).getDeclaredConstructor());
		assertNotNull(ClassDelegate.forClass(Mock3.class).getDeclaredConstructor());
		try{
			assertNotNull(ClassDelegate.forClass(Mock2.class).getDeclaredConstructor());
			fail();
		}
		catch(NoSuchMethodException ex){}
		assertNotNull(ClassDelegate.forClass(Mock3.class)
				.getDeclaredConstructor(Mock3.class));
		try{
			assertNotNull(ClassDelegate.forClass(Mock3.class)
					.getDeclaredConstructor(Exception.class));
			fail();
		}
		catch(NoSuchMethodException ex){}
	}
	
	public void testNewInstance() throws Exception{
		Mock3 mock = ClassDelegate.forClass(Mock3.class)
			.getDeclaredConstructor().newInstance();
		assertNotNull(mock);
		Constructor<Mock3> c = ClassDelegate.forClass(Mock3.class)
			.getDeclaredConstructor(Mock3.class);
		try{
			ClassDelegate.forClass(Mock3.class)
				.getDeclaredConstructor(Mock3.class).newInstance(mock);
			fail();
		}
		catch(IllegalAccessException ex){}
		c.setAccessible(true);
		assertNotNull(c.newInstance(mock));
		
		try{
			c.newInstance("");
		}
		catch(IllegalArgumentException ex){}
		try{
			c.newInstance("",mock);
		}
		catch(IllegalArgumentException ex){}
		try{
			c.newInstance();
		}
		catch(IllegalArgumentException ex){}
	}
}
